
if exists(select name from sysobjects where name = 'sp_SanPhamCreateEdit')
	drop proc sp_SanPhamCreateEdit
go
/*
	Created by: Phuoc NH
	Created on: 13/05/2013 (dd/mm/yyyy)
	Description: 
	--

	File:$sql-scripts/Proc.sp_SanPhamCreateEdit.sql
*/


-- =============================================
-- Author		: HP
-- Create date	: 2014-03-15
-- =============================================

CREATE PROCEDURE sp_SanPhamCreateEdit
			
	@Id           		INT,
	@TenSanPham   		NVARCHAR(256),
	@MoTa         		NVARCHAR(1024) ='',
	@NoiDung      		NVARCHAR(max) ='',
	@GiaBan       		INT =0,
	@GiaKM        		INT =0,
	@ThuTu        		INT =0,
	@Image        		VARCHAR(128)='',
	@Image1       		VARCHAR(128)='',
	@Image2       		VARCHAR(128)='',
	@Image3       		VARCHAR(128)='',
	@Image4       		VARCHAR(128)='',
	@Keywords     		NVARCHAR(300)='',
	@Description  		NVARCHAR(300)='',
	@Title        		NVARCHAR(300)='',
	@NguoiTao     		INT=0,
	@NguoiSua     		INT =0,
	@TrangThai    		TINYINT =0,
	@LoaiSanPhamId		INT=0
	
AS
BEGIN
    IF(@Id=0)
        BEGIN
        	INSERT INTO SanPham
        	(
		[TenSanPham],
		[MoTa],
		[NoiDung],
		[GiaBan],
		[GiaKM],
		[ThuTu],
		[Image],
		[Image1],
		[Image2],
		[Image3],
		[Image4],
		[Keywords],
		[Description],
		[Title],
		[NguoiTao],
		[NguoiSua],
		[TrangThai],
		[LoaiSanPhamId]
        	) 
        	VALUES 
        	(
		@TenSanPham,
		@MoTa,
		@NoiDung,
		@GiaBan,
		@GiaKM,
		@ThuTu,
		@Image,
		@Image1,
		@Image2,
		@Image3,
		@Image4,
		@Keywords,
		@Description,
		@Title,
		@NguoiTao,
		@NguoiSua,
		@TrangThai,
		@LoaiSanPhamId
        	)
        END
     ELSE
        BEGIN
        if(@Image<>'')
			begin
				update SanPham set [Image]=@Image where  [Id]       = @Id;
			end
		if(@Image1<>'')
			begin
				update SanPham set Image1=@Image1 where  [Id]       = @Id;
			end
            UPDATE SanPham     
            SET
		[TenSanPham]    = @TenSanPham,
		[MoTa]          = @MoTa,
		[NoiDung]       = @NoiDung,
		[GiaBan]        = @GiaBan,
		[GiaKM]         = @GiaKM,
		[ThuTu]         = @ThuTu,		
		[Image2]        = @Image2,
		[Image3]        = @Image3,
		[Image4]        = @Image4,
		[Keywords]      = @Keywords,
		[Description]   = @Description,
		[Title]         = @Title,
		[NguoiTao]      = @NguoiTao,
		[NguoiSua]      = @NguoiSua,
		[NgaySua]       = GETDATE(),
		[TrangThai]     = @TrangThai,
		[LoaiSanPhamId] = @LoaiSanPhamId
	WHERE
		    [Id]       = @Id;
        END
END
GO


